package com.yry.timertask.chapter41;

import org.junit.jupiter.api.Test;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;

public class MyTimerQx {

	@Test
	public void scheduleTest(){
		//1、创建一个timer实例
		Timer timer = new Timer();
//		 2、创建一个MyTimerTask实例
		MyTimerTaskQx myTimerTask1= new MyTimerTaskQx("No.1",2000L);
		MyTimerTaskQx myTimerTask2= new MyTimerTaskQx("No.2",2000L);

		/*获取当前时间，
		 * */
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println("Current exec time is:"+sf.format(calendar.getTime()));


 		 timer.schedule(myTimerTask1,calendar.getTime());
 		 timer.schedule(myTimerTask2,calendar.getTime());

		try {
			Thread.sleep(8000L);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}


	@Test
	public void scheduleAtTest(){
		//1、创建一个timer实例
		Timer timer = new Timer();
//		 2、创建一个MyTimerTask实例
		MyTimerTaskQx myTimerTask1= new MyTimerTaskQx("No.1",2000L);
		MyTimerTaskQx myTimerTask2= new MyTimerTaskQx("No.2",2000L);

		/*获取当前时间，
		 * */
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println("Current exec time is:"+sf.format(calendar.getTime()));


 		 timer.scheduleAtFixedRate(myTimerTask1,calendar.getTime(),2000L);
 		 timer.scheduleAtFixedRate(myTimerTask2,calendar.getTime(),2000L);

		try {
			Thread.sleep(8000L);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

 }
